﻿namespace MyDateTime
{
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public class MyDateTimeHelper
    {
        /// <summary>
        /// 当前时间
        /// </summary>
        public static DateTime CurrentDate => DateTime.Now;

        /// <summary>
        /// 获取当前时间 年月日
        /// </summary>
        /// <returns>返回当前时间 年-月-日</returns>
        public static string CurrentDateTime => DateTime.Now.ToString("yyyy-MM-dd");

        /// <summary>
        /// 获取当前时间 年月日时
        /// </summary>
        /// <returns>返回当前时间 年-月-日 时</returns>
        public static string CurrentHourDateTime => DateTime.Now.ToString("yyyy-MM-dd HH");

        /// <summary>
        /// 获取当前时间 年月日时分
        /// </summary>
        /// <returns>返回当前时间 年-月-日 时:分</returns>
        public static string CurrentMinuteDateTime => DateTime.Now.ToString("yyyy-MM-dd HH:mm");

        /// <summary>
        /// 获取当前时间 年月日时分秒
        /// </summary>
        /// <returns>返回当前时间 年-月-日 时:分:秒</returns>
        public static string CurrentSecondDateTime => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 获取当前时间 年月日时分秒毫秒
        /// </summary>
        /// <returns>返回当前时间 年-月-日 时:分:秒.毫秒</returns>
        public static string CurrentMillisecondDateTime => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

        /// <summary>
        /// 当前时间的时间戳 到日 4位
        /// </summary>
        /// <returns>返回当前时间的时间戳 4位（年月日）</returns>
        public static string CurrentTimestamp
        {
            get
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                return Convert.ToInt64(ts.TotalDays).ToString();
            }
        }

        /// <summary>
        /// 当前时间的时间戳 到小时 6位
        /// </summary>
        /// <returns>返回当前时间的时间戳 6位（年月日时）</returns>
        public static string CurrentHourTimestamp
        {
            get
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                return Convert.ToInt64(ts.TotalHours).ToString();
            }
        }

        /// <summary>
        /// 当前时间的时间戳 到分钟 8位
        /// </summary>
        /// <returns>返回当前时间的时间戳 8位（年月日时分）</returns>
        public static string CurrentMinuteTimestamp
        {
            get
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                return Convert.ToInt64(ts.TotalMinutes).ToString();
            }
        }

        /// <summary>
        /// 当前时间的时间戳 到秒 10位
        /// </summary>
        /// <returns>返回当前时间的时间戳 10位（年月日时分秒）</returns>
        public static string CurrentSecondTimestamp
        {
            get
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                return Convert.ToInt64(ts.TotalSeconds).ToString();
            }
        }

        /// <summary>
        /// 当前时间的时间戳 到毫秒 13位
        /// </summary>
        /// <returns>返回当前时间的时间戳 13位（年月日时分秒毫秒）</returns>
        public static string CurrentMillisecondTimestamp
        {
            get
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                return Convert.ToInt64(ts.TotalMilliseconds).ToString();
            }
        }

        /// <summary>
        /// 获取当前月最后一天
        /// </summary>
        /// <param name="yearMonth">传入 当前月 yyyy-MM </param>
        /// <returns>时间</returns>
        public static DateTime GetCurrentMonthLastDay(string yearMonth)
        {
            try
            {
                DateTime dateTime = Convert.ToDateTime($"{yearMonth}-01").AddMonths(1).AddDays(-1);
                return dateTime;
            }
            catch (Exception ex)
            {
                throw new Exception($"【获取当前月最后一天失败,ConvertLongToDateString】{ex.Message}");
            }
        }

        /// <summary>
        /// 时间戳转 时分秒
        /// </summary>
        /// <param name="timestamp">传入的时间戳</param>
        /// <returns>时间</returns>
        public static string ConvertLongToTimeString(long timestamp)
        {
            if (timestamp == 0)
                return "0";
            try
            {
                TimeSpan ts = TimeSpan.FromMilliseconds(timestamp);
                return ts.ToString("hh\\:mm\\:ss");
            }
            catch (Exception ex)
            {
                throw new Exception($"【时间戳转换时间失败,ConvertLongToDateString】{ex.Message}");
            }
        }

        /// <summary>
        /// 时间戳转 时分秒
        /// </summary>
        /// <param name="timestamp">传入的时间戳</param>
        /// <returns>时间</returns>
        public static string ConvertLongToTimeString(double seconds)
        {
            if (seconds == 0)
                return "0";
            try
            {
                TimeSpan ts = TimeSpan.FromSeconds(seconds);
                return ts.ToString("hh\\:mm\\:ss");
            }
            catch (Exception ex)
            {
                throw new Exception($"【时间戳转换时间失败,ConvertLongToDateString】{ex.Message}");
            }
        }

        /// <summary>
        /// 时间戳转 总秒数
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static double ConvertLongToSecond(long timestamp)
        {
            if (timestamp == 0)
                return 0;
            try
            {
                TimeSpan ts = TimeSpan.FromMilliseconds(timestamp);
                return ts.TotalSeconds;
            }
            catch (Exception ex)
            {
                throw new Exception($"【时间戳转换时间失败,ConvertLongToDateString】{ex.Message}");
            }
        }

        /// <summary>
        /// 时间戳转年月日,默认返回 年月日
        /// </summary>
        /// <param name="timestamp">传入的时间戳</param>
        /// <param name="dateType">要得到的日期类型,1 年月日时分秒；2 年月日时分；3年月日 时</param>
        /// <returns>时间</returns>
        public static string ConvertLongToDateString(string timestamp, int dateType = 0)
        {
            if (string.IsNullOrEmpty(timestamp))
            {
                return string.Empty;
            }
            string result;
            try
            {
                DateTime dateNow = GetDateTime(timestamp);
                string strFormat = GetFormat(dateType);
                result = dateNow.ToString(strFormat);
            }
            catch (Exception ex)
            {
                throw new Exception($"【时间戳转换时间失败,ConvertLongToDateString】{ex.Message}");
            }

            return result;
        }

        /// <summary>
        /// 年月日转时间戳,默认返回 到秒的转换 10位
        /// </summary>
        /// <param name="dateString">传入的时间</param>
        /// <param name="timestampType">要得到的时间戳长度,4 年月日；6 年月日时；8 年月日时分；10 年月日时分秒；13 年月日时分秒毫秒</param>
        /// <returns>string</returns>
        public static string ConvertDateStringToLong(string dateString, int timestampType = 0)
        {
            if (string.IsNullOrEmpty(dateString))
            {
                return string.Empty;
            }
            string result;
            try
            {
                DateTime dateNow = Convert.ToDateTime(dateString);
                TimeSpan timeSpan = dateNow - new DateTime(1970, 1, 1, 8, 0, 0, 0);
                switch (timestampType)
                {
                    case 4:
                        result = Convert.ToInt64(timeSpan.TotalDays).ToString();
                        break;
                    case 6:
                        result = Convert.ToInt64(timeSpan.TotalHours).ToString();
                        break;
                    case 8:
                        result = Convert.ToInt64(timeSpan.TotalMinutes).ToString();
                        break;
                    case 10:
                        result = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
                        break;
                    case 13:
                        result = Convert.ToInt64(timeSpan.TotalMilliseconds).ToString();
                        break;
                    default:
                        result = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"【年月日转时间戳失败,ConvertDateStringToLong】{ex.Message}");
            }

            return result;
        }

        #region 类内部使用方法

        /// <summary>
        /// 获取时间格式
        /// </summary>
        /// <param name="dateType">时间转换类型</param>
        /// <returns>日期格式</returns>
        private static string GetFormat(int dateType)
        {
            string strFormat = "yyyy-MM-dd";
            switch (dateType)
            {
                case -1:
                    strFormat = "M.dd";
                    break;
                case 1:
                    strFormat = "yyyy-MM-dd HH:mm:ss";
                    break;
                case 2:
                    strFormat = "yyyy-MM-dd HH:mm";
                    break;
                case 3:
                    strFormat = "yyyy-MM-dd HH";
                    break;
                default:
                    break;
            }

            return strFormat;
        }

        /// <summary>
        /// 获取DateTime
        /// </summary>
        /// <param name="timestamp">时间戳</param>
        /// <returns>时间</returns>
        private static DateTime GetDateTime(string timestamp)
        {
            try
            {
                DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0, 0);
                DateTime dateNow = new DateTime();
                long timeLong = long.Parse(timestamp);
                switch (timestamp.Length)
                {
                    case 4:
                        dateNow = dateStart.AddDays(timeLong);
                        break;
                    case 6:
                        dateNow = dateStart.AddHours(timeLong);
                        break;
                    case 8:
                        dateNow = dateStart.AddMinutes(timeLong);
                        break;
                    case 10:
                        dateNow = dateStart.AddSeconds(timeLong);
                        break;
                    case 13:
                        dateNow = dateStart.AddMilliseconds(timeLong);
                        break;
                    default:
                        break;
                }
                return dateNow;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion 类内部使用方法
    }
}
